package com.atguigu.gulimall.seckill.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.seckill.config.SeckillSentinelUrlBlockHandler;
import com.atguigu.gulimall.seckill.service.SeckillService;
import com.atguigu.gulimall.seckill.to.SeckillSkuRedisTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class SeckillController {

    @Autowired
    SeckillService seckillService;

    /**
     * 返回当前时间可以参与秒杀商品信息
     * 可以对资源限流，对url限流
     * 对资源限流，资源为@SentinelResource中的值，如果加了@SentinelResource 则必须要有兜底方法，否则会报错；
     *      blockHandlerClass只执行不满足sentinel配置的规则，如果是业务异常不会处理
     *      异常处理使用fallbackClass指定类，fallback指定方法
     * 对url限流 没兜底执行默认，也可设置全局修改默认为我们自己的
     * @return
     */
    @GetMapping("/currentSeckillSkus")
//    @SentinelResource(value = "currentSeckillSkus",
//            blockHandlerClass = SeckillSentinelUrlBlockHandler.class,
//            blockHandler = "handlerException1")
    public R getCurrentSeckillSkus(){
        List<SeckillSkuRedisTo> redisTos=seckillService.getCurrentSeckillSkus();
        return R.ok().setDate(redisTos);
    }

    @GetMapping("/hello")
    public R hello(){
        List<SeckillSkuRedisTo> redisTos=seckillService.getCurrentSeckillSkus();
        return R.ok().setDate(redisTos);
    }

    @GetMapping("/sku/seckill/{skuId}")
    public R getSkuSeckillInfo(@PathVariable("skuId") Long skuId){
//        try {
//            Thread.sleep(300);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        SeckillSkuRedisTo to=seckillService.getSkuSeckillInfo(skuId);
        return R.ok().setDate(to);
    }

    @GetMapping("/kill")
    public R seckill(@PathVariable("killId") String killId,
                     @PathVariable("key") String key,
                     @PathVariable("num") Integer num){

        String orderSn=seckillService.kill(killId,key,num);
        return null;

    }

}
